package com.znf.base.support;

import cn.hutool.http.ContentType;
import com.alibaba.fastjson.JSON;
import com.znf.base.Result;
import com.znf.base.enums.ResultCodeEnum;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JsonAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType(ContentType.JSON.getValue());
        response.getWriter().print(JSON.toJSONString(new Result(ResultCodeEnum.UNAUTHORIZED)));
    }
}
